package com.glsc.ngateway.platform.service.costallocation;

import cn.hutool.core.collection.CollectionUtil;
import com.glsc.ngateway.platform.domain.costallocation.HisIpDetailInfo;
import com.glsc.ngateway.platform.domain.costallocation.IpDetailInfo;
import com.glsc.ngateway.platform.repo.costallocation.HisIpDetailInfoRepo;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author guoj
 * @description: 历史应用详情明细
 * @date 2024/8/21 11:06
 */
@Service
public class HisIpDetailInfoService {
    @Autowired
    private HisIpDetailInfoRepo hisIpDetailInfoRepo;
    public Map<Long, List<HisIpDetailInfo>> queryByHisIpInfoIdSet(Set<Long> ipInfoIdSet, Map<String, Object> paramIpDetail){
        if (CollectionUtil.isEmpty(ipInfoIdSet)) {
            return new HashMap<>();
        }
        List<HisIpDetailInfo> toGroup;
        List<HisIpDetailInfo> ipDetailInfoList = hisIpDetailInfoRepo.queryByHisIpInfoIds(ipInfoIdSet);
        if (ipDetailInfoList.size() != 0) {
            List<HisIpDetailInfo> all = hisIpDetailInfoRepo.findAll(SpecificationUtil.buildSpecification(paramIpDetail, HisIpDetailInfo.class));
            if(CollectionUtil.isNotEmpty(all)){
                Set<Long> collect = all.stream().map(HisIpDetailInfo::getIpInfoId).collect(Collectors.toSet());
                //取交集
                ipInfoIdSet.retainAll(collect);
                toGroup = ipDetailInfoList.stream().filter(x -> ipInfoIdSet.contains(x.getIpInfoId())).collect(Collectors.toList());
            }else{
                toGroup = new ArrayList<>();
            }
        }else{
            //此时子类无查询字段  按照父类查询
            toGroup = ipDetailInfoList;
        }
        //toGroup = setGradeOneTwoNameAndLeaderName(toGroup);
        Map<Long, List<HisIpDetailInfo>> ipInfoId2List = toGroup.stream()
                .collect(Collectors.groupingBy(HisIpDetailInfo::getIpInfoId));
        return ipInfoId2List;
    }
}
